package service.Impl;

import context.BaseContext;
import dao.Impl.DishDaoImpl;
import dao.Impl.SetmealDaoImpl;
import dao.Impl.ShopCartDaoImpl;
import model.entity.Dish;
import model.entity.Setmeal;
import model.entity.ShoppingCart;
import service.ShopCartService;
import util.NowTime;

import java.util.List;

public class ShopCartServiceImpl implements ShopCartService {
    ShopCartDaoImpl shopCartDao =new ShopCartDaoImpl();
    SetmealDaoImpl setmealDao=new SetmealDaoImpl();
    DishDaoImpl dishDao=new DishDaoImpl();
    @Override
    public List<ShoppingCart> get(int id) {
        return shopCartDao.get(id);
    }

    @Override
    public void delete(Integer dishId, Integer setmealId) {
        int userId=BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCarts=shopCartDao.list(userId,dishId,setmealId);
        ShoppingCart shoppingCart=shoppingCarts.get(0);
        if(shoppingCart.getNumber()>1){
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shopCartDao.updateNumberById(shoppingCart);
        }else{
            shopCartDao.deleteById(shoppingCart.getId());
        }
    }

    @Override
    public void add(Integer dishId,Integer setmealId) {
        int userId=BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCarts=shopCartDao.list(userId,dishId,setmealId);
        if(shoppingCarts!=null&&shoppingCarts.size()>0){
            ShoppingCart shoppingCart=shoppingCarts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shopCartDao.updateNumberById(shoppingCart);
        }else {
            ShoppingCart shoppingCart = new ShoppingCart();
            if(dishId==null){
                //加入的是套餐
                Setmeal setmeal=setmealDao.getById(setmealId);
                shoppingCart.setNumber(1);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setUser_id(userId);
                shoppingCart.setSetmeal_id(setmealId);
                shoppingCart.setCategory_id(setmeal.getCategory_id());
                shoppingCart.setCreate_time(new NowTime().getTime());
                shoppingCart.setAmount(setmeal.getPrice());

            }else{
                //加入的是菜品
                Dish dish=dishDao.getById(dishId);
                shoppingCart.setNumber(1);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setUser_id(userId);
                shoppingCart.setDish_id(dishId);
                shoppingCart.setCategory_id(dish.getCategory_id());
                shoppingCart.setCreate_time(new NowTime().getTime());
                shoppingCart.setAmount(dish.getPrice());
            }
            shopCartDao.insert(shoppingCart);
        }
//        Dish d= shopCartDao.find(dishId);
//        List<Integer> list= shopCartDao.getId(dishId);
//        int t=1;
//        for (int n:list){
//            if(n==dishId)
//                t=0;
//        }
//        //如果购物车没有该商品，则新增
//        if(t==1){
//            System.out.println("正在新增菜品...");
//            shopCartDao.add(d.getName(),d.getImage(), BaseContext.getCurrentId(),d.getId(),setmealId,1,d.getPrice());
//        }
//        //有则数量+1,然后修改总价
//        else {
//            System.out.println("正在增加菜品数量...");
//            shopCartDao.addOne(dishId);
//            int a= shopCartDao.getNum(dishId);
//            shopCartDao.upAmount(dishId,a*d.getPrice());
//        }
    }

    @Override
    public void delAll(int userId) {
        shopCartDao.delAll(userId);
    }
}
